package com.nengjiang.wsplayer.activity;

import android.Manifest;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;

import com.nengjiang.wsplayer.R;
import com.nengjiang.wsplayer.base.BaseThemeActivity;
import com.nengjiang.wsplayer.oksocket.OkManager;
import com.tbruyelle.rxpermissions2.RxPermissions;

public class
WelcomeActivity extends BaseThemeActivity {

    private Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);
        handler = new Handler(Looper.getMainLooper());
        OkManager.getInstance().connect();
        requestPermissions();
    }


    private void requestPermissions() {
        if (Build.VERSION.SDK_INT >= 23) {
            RxPermissions rxPermissions = new RxPermissions(this);
            rxPermissions.request(
                    Manifest.permission.READ_EXTERNAL_STORAGE,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE,
                    Manifest.permission.CAMERA,
                    Manifest.permission.RECORD_AUDIO
            ).subscribe(aBoolean -> {
                if (!aBoolean) {
                    showToast("没有权限将影响应用部分功能使用");
                }

                goLoginDelayed();

            });
        } else {
            goLoginDelayed();
        }
    }

    private void goLoginDelayed(){
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                goLogin();
            }
        }, 2000);
    }


    private void goLogin() {
        Intent intent = new Intent(this, LoginActivity.class);
        startActivity(intent);
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (handler != null) {
            handler.removeCallbacksAndMessages(null);
        }
    }
}